package ztc.servlet;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import ztc.service.QRCodeMaker;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.util.Hashtable;

/**
 * Created by ztc on 16-4-25.
 */
public class QRApkurl extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession(false);
        ServletContext application=this.getServletContext();
        String myurl=request.getScheme()+"://"
                +request.getServerName()+":"
                +request.getServerPort()+"/qr.apk";
        try {
            Hashtable<EncodeHintType,String> hints =
                    new Hashtable<EncodeHintType,String>();
            // 内容所使用编码
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(
                    myurl, BarcodeFormat.QR_CODE,400,400,hints);
            new QRCodeMaker(Color.white, Color.black)
                    .writeToStream(bitMatrix, response.getOutputStream(), "gif",application,session);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}
